{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Correct rendering of the code in markdown cell need python-markdown notebook extension to be installed. The installation process may be a bit unclear at this time in the doc. Here is probably the most useful source of information to get it install: https://stackoverflow.com/questions/48655061/error-enabling-python-markdown-extension-for-jupyter-notebooks\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "#!pip install -q https://github.com/ipython-contrib/IPython-notebook-extensions/tarball/master --user\n",
    "#!jupyter nbextension install path_to/nbextensions/python-markdown/main.js --user\n",
    "#!jupyter nbextension enable python-markdown/main --user\n",
    "\n",
    "from notebook.nbextensions import check_nbextension\n",
    "print(check_nbextension('python-markdown', user=True))\n",
    "#print(check_nbextension('python-markdown/main'))\n",
    "#print(check_nbextension('usability/python-markdown/main'))\n",
    "\n",
    "#ext_require_path = 'usability/python-markdown/main'\n",
    "#\n",
    "#try:  # notebook >= 4.2.0\n",
    "#    from notebook.nbextensions import disable_nbextension\n",
    "#    disable_nbextension('notebook', ext_require_path)\n",
    "#except ImportError:\n",
    "#    from notebook.nbextensions import DisableNBExtensionApp\n",
    "#    DisableNBExtensionApp().disable_nbextension(ext_require_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%cd ./neurocuratorDB\n",
    "#!git pull origin master\n",
    "#%cd .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from nat.annotationSearch import AnnotationGetter, ParameterGetter\n",
    "annotGetter = AnnotationGetter(pathDB=\"neurocuratorDB\")\n",
    "paramGetter = ParameterGetter(pathDB=\"neurocuratorDB\")\n",
    "\n",
    "def citeAnnot(annotID):\n",
    "    annot = annotGetter.getAnnot(annotID)\n",
    "    pubId = annot.pubId\n",
    "    return '\"<I>' + annot.text.replace(\"\\n\", \" \") + '</I>\" (annot ID: ' + annotID + ')'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Markdown code \n",
    "\n",
    "    * (Reinhold et al, 2015) Distinct recurrent versus afferent dynamics in cortical visual processing,  http://www.nature.com/neuro/journal/v18/n12/full/nn.4153.html\n",
    "\n",
    "    * (Crandall et al, 2015) A corticothalamic switch: Controling the thalamus with dynamic synapses, http://www.cell.com/neuron/abstract/S0896-6273%2815%2900264-0 \n",
    "        * Trains of CT spikes produce initial inhibition and late excitation in TC cells\n",
    "        * The monosynaptic excitation of TC cells by CT cells shows facilitation\n",
    "            * {{\"{{\"}}citeAnnot(\"1377836a-6b89-11e6-a6a4-64006a4c56ef\")}}\n",
    "        * The disynaptic inhibition (through TRN cells) of TC cells by CT cells shows depression\n",
    "            * {{\"{{\"}}citeAnnot(\"7ca269c2-6b89-11e6-a6a4-64006a4c56ef\")}}\n",
    "        * tend to be inhibitory at low frequency (<= 0.1 Hz)\n",
    "        * tend to be excitatory at high frequency (~10 Hz, ~80 Hz)\n",
    "\n",
    "\n",
    "\n",
    "### Markdown rendering\n",
    "* (Reinhold et al, 2015) Distinct recurrent versus afferent dynamics in cortical visual processing,  http://www.nature.com/neuro/journal/v18/n12/full/nn.4153.html\n",
    "\n",
    "* (Crandall et al, 2015) A corticothalamic switch: Controling the thalamus with dynamic synapses, http://www.cell.com/neuron/abstract/S0896-6273%2815%2900264-0 \n",
    "    * Trains of CT spikes produce initial inhibition and late excitation in TC cells\n",
    "    * The monosynaptic excitation of TC cells by CT cells shows facilitation\n",
    "        * {{citeAnnot(\"1377836a-6b89-11e6-a6a4-64006a4c56ef\")}}\n",
    "    * The disynaptic inhibition (through TRN cells) of TC cells by CT cells shows depression\n",
    "        * {{citeAnnot(\"7ca269c2-6b89-11e6-a6a4-64006a4c56ef\")}}\n",
    "    * tend to be inhibitory at low frequency (<= 0.1 Hz)\n",
    "    * tend to be excitatory at high frequency (~10 Hz, ~80 Hz)\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import quantities as pq\n",
    "import matplotlib.pyplot as plt\n",
    "import collections\n",
    "\n",
    "def getFigTrace(param, context=None, index=0):\n",
    "    if context is None:\n",
    "        fig, axes = plt.subplots()\n",
    "    else:\n",
    "        fig, axarr = context\n",
    "        if isinstance(axarr, collections.Iterable):\n",
    "            axes = axarr[index]\n",
    "        else:\n",
    "            axes = axarr\n",
    "        \n",
    "    axes.plot(param.indepValues[0], param.values, '-o')\n",
    "    axes.set_xlim([-2.5, 32.5])\n",
    "    axes.set_ylabel(param.name + \" (\" + param.unit + \")\")\n",
    "    axes.set_xlabel(param.indepNames[0] + \" (\" + param.indepUnits[0] + \")\")\n",
    "    return fig    \n",
    "\n",
    "\n",
    "#### (Mooney and Miller, 2007) paper\n",
    "f, axarr = plt.subplots(1, 2,  figsize=(10,4))\n",
    "\n",
    "# Interpolate at P14 and express in mm³ units\n",
    "param = paramGetter.getParam(\"0ebdc338-6d38-11e6-b432-64006a4c56ef\")\n",
    "mooneyDensity = pq.Quantity(param.getInterp1dValues(14, \"age\"), param.unit).rescale('mm^-3')\n",
    "figMooneyDensity = getFigTrace(param, (f, axarr), 0)\n",
    "\n",
    "# Interpolate at P14\n",
    "param = paramGetter.getParam(\"90c530b4-6e05-11e6-873d-64006a4c56ef\")\n",
    "mooneyVolume = pq.Quantity(param.getInterp1dValues(14, \"age\"), param.unit)\n",
    "figMooneyVolume = getFigTrace(param, (f, axarr), 1)\n",
    "\n",
    "f.savefig('mooneyStereology.png', bbox_inches='tight')\n",
    "\n",
    "\n",
    "#### (Arcelli et al, 1997) paper\n",
    "param = paramGetter.getParam(\"4330e61a-4d01-11e6-a223-64006a4c56ef\")\n",
    "arcelliDensity = pq.Quantity(param.values, param.unit)\n",
    "\n",
    "\n",
    "#### (Cavdar et al, 2014)\n",
    "cavdarPropInterLGN = paramGetter.getParam(\"22e29fd2-6ec3-11e6-8097-64006a4c56ef\").valuesText(withUnit=True)\n",
    "cavdarPropInterVPM = paramGetter.getParam(\"614cab12-6ecf-11e6-9428-64006a4c56ef\").valuesText(withUnit=True)\n",
    "cavdarPropInterVPL = paramGetter.getParam(\"8ca0b484-6ecf-11e6-9428-64006a4c56ef\").valuesText(withUnit=True)\n",
    "\n",
    "def rescale2DStereo(paramID, thicknessValue=1.0, thicknessUnit=\"um\", desiredUnit=\"mm^-3\"):\n",
    "    density = paramGetter.getParam(paramID)\n",
    "    thickness = pq.Quantity(thicknessValue, thicknessUnit)\n",
    "    return (density/thickness).rescale(desiredUnit)\n",
    "\n",
    "cavdarDensityInterLGN = rescale2DStereo(\"8f01bcb4-6ed4-11e6-9428-64006a4c56ef\")\n",
    "cavdarDensityInterVPM = rescale2DStereo(\"026c0650-6ed5-11e6-9428-64006a4c56ef\")\n",
    "cavdarDensityInterVPL = rescale2DStereo(\"d98090e8-6ed5-11e6-9428-64006a4c56ef\")\n",
    "\n",
    "cavdarDensityRelayLGN = rescale2DStereo(\"3d43a784-6ed4-11e6-9428-64006a4c56ef\")\n",
    "cavdarDensityRelayVPM = rescale2DStereo(\"c1651732-6ed4-11e6-9428-64006a4c56ef\")\n",
    "cavdarDensityRelayVPL = rescale2DStereo(\"8a58a41a-6ed5-11e6-9428-64006a4c56ef\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "*   From literature:\n",
    "    * From [(Barbaresi et al., 1986)](http://www.ncbi.nlm.nih.gov/pubmed/2428443): \n",
    "        * {{citeAnnot(\"7e94d3d8-6d27-11e6-a69d-64006a4c56ef\")}} \n",
    "        * Reports virtually no GABAergic neurons in VP, some rare near the VP-PO junction. (ID: 384fe902-6d28-11e6-a69d-64006a4c56ef)\n",
    "\n",
    "    * From [(Mooney and Miller, 2007)](http://www.ncbi.nlm.nih.gov/pubmed/17494688) :\n",
    "        * {{citeAnnot(\"a0e2cdfc-5277-11e6-9702-64006a4c56ef\")}} [measure in VB]\n",
    "        \n",
    "        * Interpolating for P14 from Figure 3 of the paper (see below for reproduction with annotated values) and expressing it in mm⁻³, we get (for VB; stained with cresyl violet or immunolabeled with NeuN):\n",
    "            * Density (top panel) : {{mooneyDensity}}\n",
    "            * Volume (middle panel): {{mooneyVolume}}\n",
    "            * Number of neurons (Density X Volume): {{mooneyDensity*mooneyVolume}}\n",
    "            * Annotated values from Fig. 3 ([click here](http://www.jneurosci.org/content/27/19/5023/F3.large.jpg) for the original figure):\n",
    "            <img src=\"http://www.jneurosci.org/content/27/19/5023/F3.large.jpg\" width=\"300\"></img><BR><BR>\n",
    "        \n",
    "    * From [(Arcelli et al, 1997)](http://www.ncbi.nlm.nih.gov/pubmed/8978932) **<span style=\"color:rgb(255,0,0);\">(age unknown; counting method not great and not allowing passing from 2D to 3D)</span>** :\n",
    "        *   Density in the reticular nucleus: {{arcelliDensity}}\n",
    "        *   {{citeAnnot(\"d972caee-6eb2-11e6-9470-64006a4c56ef\")}} [mouse and rat]\n",
    "        *   Density of interneurons in LG: 15–20% [{{citeAnnot(\"451c913e-a7d6-11e5-b8b9-64006a4c56ef\")}}]\n",
    "        *   Density of interneurons in other nuclei: <1% [{{citeAnnot(\"83cd7f72-6eb1-11e6-9470-64006a4c56ef\")}}]\n",
    "\n",
    "    *   From [(Cavdar et al, 2014)](http://www.ncbi.nlm.nih.gov/pubmed/24526659) (Wistar rats; 6–12 months old; light microscopy immunocytochemistry with a GABA immunogold marker; Counting method used: disector; full table containing the following data can be [https://s31.postimg.org/e2g8ja90r/Screenshot_from_2016_07_21_15_39_25.png](viewed here)):\n",
    "        * Proportion of interneurons, \n",
    "            *   LGN: {{cavdarPropInterLGN}}\n",
    "            *   VPM: {{cavdarPropInterVPM}}\n",
    "            *   VPL: {{cavdarPropInterVPL}}\n",
    "        * Given the use of disector for obtaining surface densities, volume densities can be computed by from area by dividing by the tickeness of the disector which is 1 um in this studies. Thus, we have for the Wistar rat:\n",
    "    \n",
    "            * Interneuron density ($N_i$):\n",
    "                * LGN: {{cavdarDensityInterLGN.valuesText(True)}}\n",
    "                * VPM: {{cavdarDensityInterVPM.valuesText(True)}}\n",
    "                * VPL: {{cavdarDensityInterVPL.valuesText(True)}}\n",
    "\n",
    "            * Relay cell density ($N_{tc}$):\n",
    "                * LGN: {{cavdarDensityRelayLGN.valuesText(True)}}\n",
    "                * VPM: {{cavdarDensityRelayVPM.valuesText(True)}} \n",
    "                * VPL: {{cavdarDensityRelayVPL.valuesText(True)}}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv3.4",
   "language": "python",
   "name": "venv3.4"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
